#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/11/15 15:19
# @Author : 闫峰
# @File    : api_listing.py
import requests

from common.setting import ensure_path_sep
from utils.mysqlUtils.db_pool import Database


class ApiListing:
    # db = Database(ensure_path_sep("\\config\\shopeev2_config.yaml.bak"), "test_jdw_database")
    db = Database(ensure_path_sep("\\config\\shopeev2_config.yaml.bak"),
                                                   "zhcxkj_center_listing_comprehensive")
    def apilisting(self, platformType, startTime, endTime, limit):
        count = 0
        nextOffSet = 0
        expect_listing_list = []
        while True:
            res = self.get_listing(platformType, startTime, endTime, limit,nextOffSet)
            data = list(filter(lambda x:x['itemId'] ==5477372032,res.get("data")))
            if data:
                print()
            if res.get("data"):
                count += len(res.get("data"))
                expect_listing_list += list(map(lambda x:(x['sourceId'],x['itemId'],x['sourceSku']) , res.get('data')))

                nextOffSet = res.get("data")[-1].get('listingId')
            else:
                break
        actual_listing_list = self.get_listing_by_sql(startTime,endTime)
        print(f"expect_listing_list总共{len(expect_listing_list)}")
        print(f"actual_listing_list总共{len(actual_listing_list)}")
        expect_listing_set = set(expect_listing_list)
        actual_listing_set = set(actual_listing_list)
        if expect_listing_set != actual_listing_set:
            print()



    def get_listing(self, platformType, startTime, endTime, limit, nextOffSet):
        try:
            # url = """https://webapi.internal.zhcxkj.com/open/riskListing/getListing"""
            url = """https://webapi.zhcxkj.com/open/riskListing/getListing"""
            data = {
                "startTime": startTime,
                "endTime": endTime,
                "platformType": platformType,
                "nextOffSet": nextOffSet,
                "limit": limit
            }
            headers = {
                "Authorization": "Basic emhjeGphdmE6MTIzNDU2"
            }
            res = requests.post(url=url, headers=headers, json=data).json()
            return res
        except Exception as e:
            print(res)
            raise e


    def get_listing_by_sql(self,startTime,endTime):
        get_listing_t_walmart_listing = """ 
        SELECT source_id,item_id,source_sku
        FROM
            zhcxkj_center_listing_comprehensive.t_walmart_listing 
        WHERE update_time BETWEEN %s AND  %s
        AND `status`=1;
        """
        get_listing_t_walmart_wfs_listing = """
        SELECT source_id,item_id,source_sku
        FROM
            zhcxkj_center_listing_comprehensive.t_walmart_wfs_listing 
        WHERE update_time BETWEEN  %s AND  %s
        AND `status`=1;
        """
        t_walmart_listing_list = list(self.db.fetch_all_tuple(get_listing_t_walmart_listing,(startTime,endTime)))
        t_walmart_wfs_listing_list = list(self.db.fetch_all_tuple(get_listing_t_walmart_wfs_listing,(startTime,endTime)))
        return t_walmart_listing_list + t_walmart_wfs_listing_list



if __name__ == '__main__':
    # ApiListing().apilisting(97,"2023-11-10 00:00:00","2023-11-11 00:00:00",200)
    # ApiListing().apilisting(45,"2024-01-16 05:10:59","2024-01-17 19:59:33",200)
    # ApiListing().apilisting(45,"2024-01-17 00:30:44","2024-01-17 19:59:33",200)
    # ApiListing().apilisting(45,"2024-01-01 16:29:00","2024-01-22 16:30:00",200)
    ApiListing().apilisting(45,"2024-05-14 22:00:00","2024-05-14 22:10:00",200)
    # ApiListing().get_listing_by_sql("2024-01-22 16:29:00","2024-01-23 19:59:33")
    # ApiListing().get_listing_by_sql("2024-01-22 16:29:00","2024-01-22 23:59:33")
    # ApiListing().apilisting(45,"2024-01-15 16:59:35","2024-01-15 19:59:33",200)
    # ApiListing().get_listing_by_sql("2024-01-14 16:36:50","2024-01-15 19:59:33")